package com.wolf.cx330.room

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.room.Room
import com.blankj.utilcode.util.GsonUtils
import com.wolf.cx330.R
import com.wolf.cx330.room.database.AppDataBase
import com.wolf.cx330.room.entity.MyUser
import kotlinx.android.synthetic.main.act_room.*
import java.util.*

class RoomAct : AppCompatActivity() {
    companion object {
        fun actionStart(context: Context) {
            context.startActivity(Intent(context, RoomAct::class.java))
        }
    }

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.act_room)
        val db = Room.databaseBuilder(
            applicationContext,
            AppDataBase::class.java,
            "database-name"
        )
            //  允许ROOM的主线程操作
            .allowMainThreadQueries()
            .build()
        selectBtn.setOnClickListener {
            val list = db.userDao().getAll()
            textView.text = ""
            if (list.isNotEmpty()) {
                list.forEach {
                    textView.text = "${textView.text}\n ${GsonUtils.toJson(it)}"
                }
            }
        }
        insertAllBtn.setOnClickListener {
            db.userDao().insertAll(
                MyUser(UUID.randomUUID().toString(), (Math.random() * 80).toString(), (Math.random() * 800).toString()),
                MyUser(UUID.randomUUID().toString(), (Math.random() * 80).toString(), (Math.random() * 800).toString()),
                MyUser(UUID.randomUUID().toString(), (Math.random() * 80).toString(), (Math.random() * 800).toString()),
                MyUser(UUID.randomUUID().toString(), (Math.random() * 80).toString(), (Math.random() * 800).toString()),
                MyUser(UUID.randomUUID().toString(), (Math.random() * 80).toString(), (Math.random() * 800).toString())
            )
            val list = db.userDao().getAll()
            textView.text = ""
            if (list.isNotEmpty()) {
                list.forEach {
                    textView.text = "${textView.text}\n ${GsonUtils.toJson(it)}"
                }
            }

        }
        insertBtn.setOnClickListener {
            val user = MyUser(UUID.randomUUID().toString(), (Math.random() * 80).toString(), (Math.random() * 800).toString())
            db.userDao().insertAll(user)

            val list = db.userDao().getAll()
            textView.text = ""
            if (list.isNotEmpty()) {
                list.forEach {
                    textView.text = "${textView.text}\n ${GsonUtils.toJson(it)}"
                }
            }
        }
        delBtn.setOnClickListener {
            db.userDao().clear()

            val list = db.userDao().getAll()
            textView.text = ""
            if (list.isNotEmpty()) {
                list.forEach {
                    textView.text = "${textView.text}\n ${GsonUtils.toJson(it)}"
                }
            }
        }
    }
}